import {SessionUtils} from "@/util/sessionUtils";

export const TEST_ARRANGE_APP_CONFIG_KEY = 'test-arrange-app-config'


const appConfig = getAppConfig();

import _ from 'lodash';

export function getAppConfig() {
    let appConfig = require("../app.config")
    let sessionAppConfig = SessionUtils.get(TEST_ARRANGE_APP_CONFIG_KEY)
    console.log(sessionAppConfig)
    if (sessionAppConfig) {
        return sessionAppConfig;
    }
    return appConfig;
}

export const processAppConfig = _.once(() => {
    //加载字典数据
    processColumnDefines(appConfig.columnDefines, appConfig.dicts)

    appConfig.columns = processColumn(appConfig.columnsMap, appConfig.columnDefines)

    appConfig.columnNamePropMap = processNamePropNameMap(appConfig.columnsMap)

    processExportColumnDicData(appConfig)
    console.log(appConfig)

    processExamHallDict()
    return appConfig;
})

export function processExportColumnDicData(appConfig) {
    let data = []
    for (let columnDefineKey in appConfig.columnDefines) {
        let item = appConfig.columnDefines[columnDefineKey]
        data.push({
            label: item.label,
            value: columnDefineKey
        })
    }
    appConfig.dicts['exportColumn'] = data

}

//加载考场的字典
export function processExamHallDict() {


    var examHallMap = _.reduce(appConfig.examHalls, (result, value) => {


        if (result[value["campus"]]) {
            let map = result[value["campus"]]
            map.set(value.code, value)
            result[value["campus"]] = map
        } else {
            let map = new Map()
            map.set(value.code, value)
            result[value["campus"]] = map
        }
        return result
    }, {});


    appConfig.examHallMap = examHallMap
}

//处理 name->prop映射
export function processNamePropNameMap(columnsMap) {

    let columnNamePropMap = {}
    for (let columnsMapKey in columnsMap) {
        let values = columnsMap[columnsMapKey]
        values.forEach(item => {
            columnNamePropMap[item] = columnsMapKey;
        })
    }

    return columnNamePropMap;
}

//解析columns
export function processColumn(columnsMap, columnDefines) {

    let columns = {}
    for (let columnsMapKey in columnsMap) {
        let rows = columnsMap[columnsMapKey];
        rows.forEach(item => {
            columns[item] = columnDefines[columnsMapKey]
        })
    }


    return columns;
}

//处理columnDefines  加载字典数据
export function processColumnDefines(columnDefines, dicts) {
    for (let columnDefineKey in columnDefines) {
        let item = columnDefines[columnDefineKey];
        if (item.dicData) {
            let dicData = []
            for (let i = 0; i < item.dicData.length; i++) {
                let dicItem = item.dicData[i]
                dicData = dicData.concat(dicts[dicItem])
            }
            item.dicType = item.dicData

            item.dicData = dicData;

            item.dicMap = new Map()
            for (let i = 0; i < item.dicData.length; i++) {
                item.dicMap.set(item.dicData[i].value, item.dicData[i].label)
            }


        }

    }

}

//考场按校区分组
export function processExamHallGroupByCampus() {

    var dicts = appConfig.dicts;
    var examHalls = appConfig.examHalls.map(item => {
        item.campusName = getDictValue(dicts, 'campus', item.campus);
        return item;
    })
    return _.groupBy(examHalls, 'campus');
}

export function buildExamHallTree() {
    var dictionary = processExamHallGroupByCampus();
    var treeData = []
    for (let dictionaryKey in dictionary) {
        let item = dictionary[dictionaryKey]
        let data = {
            label: getDictValue(appConfig.dicts, 'campus', dictionaryKey),
            value: dictionaryKey,
            children: item.map(sitem => {
                return {
                    label: sitem.name, value: sitem.code, ...sitem
                }
            })
        }
        treeData.push(data)
    }
    console.log(treeData)
    return treeData;
}

//获取字典映射的值
export function getDictValue(dicts, key, value) {
    let item = dicts[key].find(item => item.value === value)
    return item && item.label;
}

//获取字典映射的值
export function dictMap(dicData, value) {
    let item = dicData.find(item => item.value === value)
    return item && item.label;
}